package com.tb.controller;

import com.tb.dubbo.IGoodsCommentsService;
import com.tb.pojo.Comments;
import com.tb.pojo.Goods;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import com.tb.utils.ReturnList;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goodsComments")
public class GoodsCommentsController {

    @Autowired
    private IGoodsCommentsService goodsCommentsService;

    /**
     * @api {get} /goodsComments/select 根据商品ID查询商品所有评论 宁凯悦
     * @apiDescription 根据商品ID查询商品所有评论
     * @apiGroup Goods API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=10] 每页数据条数
     * @apiParam {String} [goodsId=0] 商品ID
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 20,
     *          "goodsId": 1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">1:未找到商品</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {String} goodsId 商品id
     * @apiSuccess  (list) {String} goodsName 商品名称
     * @apiSuccess  (list) {float} goodsPrice 商品单价
     * @apiSuccess  (list) {Comments} listComments 商品评论
     * @apiSuccess  (Comments) {int} id 评论id
     * @apiSuccess  (Comments) {String} comments 评价
     * @apiSuccess  (Comments) {int} staifaction 满意度
     * @apiSuccess  (Comments) {User} user 用户
     * @apiSuccess  (User) {int} id 用户id
     * @apiSuccess  (Comments) {String} userName 用户名
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *    {
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 1,
     *         "list": [
     *             {
     *                 "goodsId": "2",
     *                 "goodsName": "耐克上衣",
     *                 "goodsPrice": 500.0,
     *                 "listComments": [
     *                     {
     *                         "id": 2,
     *                         "comments": "衣服很好看",
     *                         "staifaction": 4,
     *                         "user": {
     *                             "id": 3,
     *                             "userName": "w***u"
     *                         }
     *                     }
     *                 ]
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "未找到商品",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /goodsComments/select
     */
    @GetMapping("/select")
    public Msg getComments(@RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "rows", defaultValue = "30") Integer rows,
                           @RequestParam(value = "goodsId", defaultValue = "0") String goodsId) {
        Map<String, Object> map = new HashMap<>();
        map.put("gid", goodsId);
        map.put("page", page);
        map.put("rows", rows);
        Map<String, Object> rmap = goodsCommentsService.goodsComments(map);
        //拿单个商品所有评论
        List<Comments> listComments;
        if (rmap != null) {
            Goods goods = (Goods) rmap.get("goods");
            long count = (long) rmap.get("count");
            listComments = goods.getListComments();
            //遍历拿到用户name
            for (Comments com : listComments) {
                User user = com.getUser();
                String userName = user.getUserName();
                String start = userName.substring(0, 1);
                String end = userName.substring(userName.length() - 1);
                String res = start + "***" + end;
                user.setUserName(res);
            }
            List<Goods> list = new ArrayList<>();
            list.add(goods);
            return Msg.success(ReturnList.get(count, list));
        } else {
            return Msg.error(1, "未找到商品");
        }
    }
}
